package com.zjj.wangluo;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;

public class BS {
    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(8888)) {
            System.out.println("服务器启动，监听端口：8888");
            System.out.println("访问地址：http://127.0.0.1:8888");

            while (true) {
                try (Socket clientSocket = serverSocket.accept()) {
                    String clientIp = clientSocket.getInetAddress().getHostAddress();
                    System.out.println("客户端连接：" + clientIp);

                    OutputStream outputStream = clientSocket.getOutputStream();
                    PrintStream out = new PrintStream(outputStream);

                    out.println("HTTP/1.1 200 OK");
                    out.println("Content-Type: text/html; charset=utf-8");
                    out.println();

                    out.println("<html><body>");
                    out.println("<h1>欢迎 " + clientIp + " 上线！</h1>");
                    out.println("</body></html>");
                } catch (IOException e) {
                    System.err.println("处理客户端请求时出错：" + e.getMessage());
                }
            }
        } catch (IOException e) {
            System.err.println("服务器启动失败：" + e.getMessage());
        }
    }
}